import os,subprocess

from lexer import Lexer;
from parser.parser import Parser;
from interpreter import Interpreter,SemanticAnalyzer;


#打印输出Lexer中的信息
def printLexer(lexer):
	token={};
	i=0;
	for tokens in lexer.lexing():
		token[i]=tokens.viewTree();
		i+=1;
		if tokens.tokenType=='EOF':
			break;

	print(token)

#读取文件夹下的所有文件
def readFolderAllFiles():
	path = 'D:/project/p_compiler/new_compiler/testcode/';

	# 源码文件
	codeList = [];
	text='';

	for file_name in os.listdir(path):
		codeList.append(file_name);
		# 读取文件中的源码
		file_text = open(path+file_name, 'r').read();
		text=text+file_text;

	return text;


if __name__ == '__main__':


# text =  '''
# 			import "./error/error.py";
# 			num=1;
# 			age=10;
# 			ss1=(num and age);
# 			ss2=(num or age);
# 			ss3=not age;
# 		''';

	# text =  '''
	# 	//var str="false";
	# 	var flag=false;
	# 	var n,factorial,elses,num=9;
	# 	var a=9;

	# 	function test(num,flag){
	# 		num=num+1;
	# 		print(num,flag);
	# 	}

	# 	test(a,flag);

	# 	//a=1+2;
	# 	if n > 0 {
	# 		factorial=200;
	# 	}

	# 	//for a=10 to 12 {
	# 		//print(a);
	# 	//}

	# 	//for var i=0;i<num;i++ {}

	# 	//print(gettype(flag));
	# 	print(flag);
	# 	print(n);
	# 	print(factorial);
	# 	print(elses);
	# 	print(num);
	# 	print(a);

	# ''';


	#读取文件夹下的所有文件
	text=readFolderAllFiles();

	# texts = input('>>>')
	lexer = Lexer(text);
	#================ start 测试 ================#
	# debug
	# print(lexer.lexing());
	# printLexer(lexer);
	# exit();
	#================ end 测试 ================#


	parser=Parser(lexer);
	#================ start 测试 ================#
	# debugb
	# node=parser.parser();
	# print(node.viewTree());
	# exit();
	#================ end 测试 ================#


	tree = parser.parser()
	# 语义分析器
	semantic_analyzer = SemanticAnalyzer()
	semantic_analyzer.visit(tree)
	# print('符号表中的内容：\n', symtab_builder.symbol_table)
	print('符号表中的内容：\n', semantic_analyzer.symbol_table.viewTree())


	print('-----------------------------------------------')
	print('开始解释代码...')
	# 实例化解释器对象
	interpreter = Interpreter(tree);
	# 执行运算方法获取运算结果
	interpreter.interpret();
	print('全局存储中的内容：')
	# for k, v in interpreter.GLOBAL_MEMORY.items():
	# 	print(f'{k}：{v}')



